Imports System.Drawing
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

#Region "Enums"
<Serializable()> Public Enum CoreRules_cgd
    None = 0
    Bridging = 1
    Engineering = 2
    [Protected] = 3
    Deployment = 4
    AirDrop = 5
    AirTransport = 6
    EmbarkSea = 7
End Enum

<Serializable()> Public Enum TurnLength_cgd
    Hour = 0
    HalfDay = 1
    FullDay = 2
    Week = 3
    Month = 4
End Enum

<Serializable()> Public Enum IconImageIndex_cgd
    Owner = 0
    Enemy = 1
    SameFaction = 2
    Allied = 3
    Neutral = 4
End Enum

<Serializable()> Public Enum RuleApplies_cgd
    Constant = 0  ' always in effect, these rules should have NO max number of uses
    Combat = 1   ' only during combat can this rule be used. If it has 0 max uses, then it is a 'constant' in any combat
    NonCombat = 2 ' may only be used during the planning phase
End Enum

<Serializable()> Public Enum CaptureEvent_cgd
    ChangeOwnership = 0
    Hold = 1
    Destroyed = 2
End Enum

<Serializable()> Public Enum BattleResult_cgd
    NotFinished = 0
    AttackerVictory = 1
    DefenderVictory = 2
    AttackerMarginalVictory = 3
    DefenderMarginalVictory = 4
    Draw = 5
End Enum

<Serializable()> Public Enum UnitBattleResult_cgd
    NoBattle = 0
    Draw = 1
    MarginalVictory = 2
    Victory = 3
    MarginalDefeat = 4
    Defeat = 5
End Enum

<Serializable()> Public Enum BattlePosture_cgd
    NoBattle = 0
    Attacking = 1
    Defending = 2
    AttackingReserve = 3
    DefendingReserve = 4
    AttackingSupport = 5
    DefendingSupport = 6
    AttackingIndirectSupport = 7
    DefendingIndirectSupport = 8
End Enum

<Serializable()> Public Enum DiplomaticStanding_cgd
    SameFaction = 0
    Allied = 1
    Neutral = 2
    Enemy = 3
End Enum

<Serializable()> Public Enum GroundUnitStatus_cgd
    OK = 0
    Engaged = 1
    Moving = 2
    Assembling = 3
    Reinforcing = 4
    Regrouping = 5
    Regrouped = 6
End Enum

<Serializable()> Public Enum GroundChitClass_cgd
    Objective = 0
    Unit = 1
    Token = 2
End Enum

Public Enum PlayerPOV_cgd
    Attacking = 1
    Defending = 2
End Enum

<Serializable()> Public Enum EventAuthorityLevel
    PublicEvent = 0  ' always shows up
    FriendlyEvent = 1   ' shows up on friendly factions
    OwningEvent = 2   ' only owning player shows up
    ModeratorEvent = 3  ' only shows for moderator
End Enum

#End Region

<Serializable()> Public Class CampaignerGameData

    Public Enum ModeratorFlag
        SealedGame = 0
        ModeratorReady = 1       '// does not HAVE to use a moderator for this campaign
        ModeratorRequired = 2
    End Enum

    Public Enum ChitDetectionLevel
        UnaccountedDL = 0
        UndetectedDL = 1
        DetectedDL = 2
        SightedDL = 4
        IdentifiedDL = 8
        KnownDL = 12
    End Enum

    ' this class is the base for a game, its properties and collections define all elements in the campaign
    Private mvarDemo As Boolean
    Private mvarGameName As String
    Private mvarVersion As String
    Private mvarTheaterMapFilename As String = "none"
    Private mvarTurn As Int16
    Private mvarTurnLength As TurnLength_cgd
    Private mvarMaxTurns As Int16
    Private mvarUsesZOC As Boolean = False
    Private mvarSystemValues As New Hashtable()
    Private m_Moderator As ModeratorFlag = ModeratorFlag.ModeratorReady
    Private m_ModeratorPassword As String = "parsons"
#If aflegacy = 1 Then
    <NonSerialized()> Private m_ReinforcementRange As Int16 = 50
    <NonSerialized()> Private m_AverageUnitMaxPV As Int32
    <NonSerialized()> Private m_AverageObjectiveMaxPV As Int32
    <NonSerialized()> Private m_MaxPointValueDeployed As Int32
#Else
    private m_ReinforcementRange as Int16 = 50
    private m_AverageUnitMaxPV as Int32
    private m_AverageObjectiveMaxPV as Int32
    private m_MaxPointValueDeployed as Int32
#End If
    '<laziness>
    ' HACK: all of this lazy junk should be moved to properties and proper collections
    Public Orders_g As New OrderTypes()
    Public Faction_g As New Factions()
    Public RuleTypes_g As New RuleTypes()
    Public Rules_g As New Rules()
    Public ResourceTypes_g As New ResourceTypes()
    Public Resources_g As New Resources()
    Public Messages_g As New Messages()
    Public DeploymentZones_g As New DeploymentZones()
    Public Battles_g As New Battles()
    Public Diplomacy_g As New Diplomacy()
    Public MapLabels_g As New MapLabels()
    Public AllChits As New Hashtable(45)        ' this holds all instances of objectives, units and tokens (NOT types)
    Public ObjectiveTypes As New Hashtable(10)
    Public UnitTypes As New Hashtable(10)
    Public TokenTypes As New Hashtable(10)
    Public selectedFaction As Faction
    Public selectedPlayer As Player
    Public SubObjectiveMaps As New SubObjectiveMaps()
    Public Players As New Hashtable(2)
    Public LogEvents As New Hashtable(10)
    '</laziness>   >:P

    ' BIGTODO: Put MOVEMENT TYPES in here, and take OUT of the flexmap.

    ' four letter key combinations for hashtable lookups
    Public Const CGDKEY_OBJECTIVETYPE = "OBJT"
    Public Const CDGKEY_RESOURCETYPE = "REST"
    Public Const CDGKEY_RULETYPE = "RULT"
    Public Const CDGKEY_UNITTYPE = "UNTT"
    Public Const CDGKEY_TOKENTYPE = "TOKT"

    Public Const CDGKEY_FACTION = "FACT"
    Public Const CDGKEY_PLAYER = "PLAY"

    Public Const CDGKEY_OBJECTIVE = "OBJE"
    Public Const CDGKEY_RESOURCE = "RESO"
    Public Const CDGKEY_RULE = "RULE"
    Public Const CDGKEY_UNIT = "UNIT"
    Public Const CDGKEY_TOKEN = "TOKN"
    Public Const CDGKEY_ASSIGNEDRULE = "ASRL"

    Public Const CDGKEY_ORDER = "ORDE"
    Public Const CDGKEY_ORDERTYPE = "ORDT"

    Public Const CDGKEY_BATTLE = "BATT"

    Public Const CDGKEY_CHITTYPE = "CHTT" ' generic chit... there shouldn't be many of these.
    Public Const CGDKEY_GROUNDCHITTYPE = "GCHT"  ' this is the main type that objective types, unit types and token types derive from.

    ' icon IDs for determining the correct display icon
    Public Const CDGDIP_OWNER = "Owner"
    Public Const CDGDIP_SAMEFACTION = "SameFaction"
    Public Const CDGDIP_ALLIED = "Allied"
    Public Const CDGDIP_NEUTRAL = "Neutral"
    Public Const CDGDIP_ENEMY = "Enemy"

#Region "Data Sets"

    Public Sub SaveDataSet()
        ' this saves all type-specific data, such as chit types, resource types, etc as CGS file. Does not save game data.
        Dim sGame As New CampaignerGameData()
        Dim setFileName As String

        ' not a valid option with a game marked as a demo
        If Me.Demo Then
            Throw New UnauthorizedAccessException()
        End If

        Dim fs As New FileStream(setFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim bin As New BinaryFormatter()

        Try
            sGame = Me.Clone

            ' delete map information
            With sGame
                .TheaterMapFilename = "none"
                .Battles_g.HT.Clear()
                .AllChits.Clear()
                .DeploymentZones_g.HT.Clear()
                .Faction_g.HT.Clear()
                .GameName = setFileName
                .MapLabels_g.HT.Clear()
                .Messages_g.HT.Clear()
            End With

            bin.Serialize(fs, sGame)

        Catch
            Kill(setFileName)
            sGame = Me.Clone
            bin.Serialize(fs, sGame)
        Finally

        End Try
    End Sub

    Public Sub LoadDataSet(ByVal setFileName As String)
        'TODO: Loading Data Sets into a game.

    End Sub

#End Region

    Public ReadOnly Property DefinedManagableCount() As Int16
        Get
            ' returns the number of orders, objective types, token types, unit types, resource types and rule types defined
            Dim j As Int16
            j = ObjectiveTypes.Count + UnitTypes.Count + TokenTypes.Count + ResourceTypes_g.Count + RuleTypes_g.Count + Orders_g.Count
            Return j
        End Get
    End Property

    Public Property MaxPointValueDeployed() As Int32
        Get
            Return m_MaxPointValueDeployed
        End Get
        Set(ByVal Value As Int32)
            m_MaxPointValueDeployed = Value
        End Set
    End Property

    Public Property AverageUnitMaxPV() As Int32
        Get
            Return m_AverageUnitMaxPV
        End Get
        Set(ByVal Value As Int32)
            m_AverageUnitMaxPV = Value
        End Set
    End Property

    Public Property AverageObjectiveMaxPV() As Int32
        Get
            Return m_AverageObjectiveMaxPV
        End Get
        Set(ByVal Value As Int32)
            m_AverageObjectiveMaxPV = Value
        End Set
    End Property

    Public Property ModeratorPassword() As String
        Get
            Return m_ModeratorPassword
        End Get
        Set(ByVal Value As String)
            m_ModeratorPassword = Value
        End Set
    End Property

    Public Property Moderator() As ModeratorFlag
        Get
            Return m_Moderator
        End Get
        Set(ByVal Value As ModeratorFlag)
            m_Moderator = Value
        End Set
    End Property

    Public Sub Save(ByVal Filepath As String)
        ' serialize and save
        Dim sGame As New CampaignerGameData()
        Try
            Dim fs As New FileStream(Filepath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)
            Dim bin As New BinaryFormatter()

            sGame = Me.Clone
            bin.Serialize(fs, sGame)
        Catch ex As System.IO.IOException
            Kill(Filepath)
            Dim fs As New FileStream(Filepath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)
            Dim bin As New BinaryFormatter()

            sGame = Me.Clone
            bin.Serialize(fs, sGame)
        Catch ex As Exception
            Throw ex
        End Try


    End Sub

    Public Function Clone() As CampaignerGameData
        Dim m As New MemoryStream()
        Dim b As New BinaryFormatter()

        ' for the nonserializable junk
        Me.DeploymentZones_g.SetPathToPoints()

        b.Serialize(m, Me)
        m.Position = 0
        Return b.Deserialize(m)

    End Function

    Public Function Open(ByVal Filepath As String) As CampaignerGameData
        Dim fs As New FileStream(Filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim bin As New BinaryFormatter()
        Dim sGame As New CampaignerGameData()

        sGame = CType(bin.Deserialize(fs), CampaignerGameData)

        ' for the nonserializable junk
        sGame.DeploymentZones_g.SetPointsToPath()

        Return sGame

    End Function

#Region "Property Declarations"
    Public Property ReinforcementRange() As Int16
        Get
            'HACK: make this return the proper value
            Return 50
        End Get
        Set(ByVal Value As Int16)
            m_ReinforcementRange = Value
        End Set
    End Property
    Public Property Demo() As Boolean
        Get
            Return mvarDemo
        End Get
        Set(ByVal Value As Boolean)
            mvarDemo = Value
        End Set
    End Property

    Public Property GameName() As String
        Get
            Return mvarGameName
        End Get
        Set(ByVal Value As String)
            mvarGameName = Value
        End Set
    End Property

    Public Property Version() As String
        Get
            Return mvarVersion
        End Get
        Set(ByVal Value As String)
            mvarVersion = Value
        End Set
    End Property

    Public Property TheaterMapFilename() As String
        Get
            Return mvarTheaterMapFilename
        End Get
        Set(ByVal Value As String)
            mvarTheaterMapFilename = Value
        End Set
    End Property

    Public Property Turn() As Int16
        Get
            Return mvarTurn
        End Get
        Set(ByVal Value As Int16)
            mvarTurn = Value
        End Set
    End Property

    Public Property TurnLength() As TurnLength_cgd
        Get
            Return mvarTurnLength
        End Get
        Set(ByVal Value As TurnLength_cgd)
            mvarTurnLength = Value
        End Set
    End Property

    Public Property MaxTurns() As Int16
        Get
            Return mvarMaxTurns
        End Get
        Set(ByVal Value As Int16)
            mvarMaxTurns = Value
        End Set
    End Property

    Public Property UsesZOC() As Boolean
        Get
            Return mvarUsesZOC
        End Get
        Set(ByVal Value As Boolean)
            mvarUsesZOC = Value
        End Set
    End Property

    Public Property SystemValue(ByVal Key As String) As String
        Get
            ' returns the item associated with the key or a "NoItem" return value
            Dim item As Object
            Try
                item = mvarSystemValues(Key)
                If item Is Nothing Then
                    Return "NoItem"
                Else
                    Return item
                End If
            Catch
                Return "NoItem"
            End Try
        End Get
        Set(ByVal Value As String)
            ' updates or add the value with the associated key
            Try
                mvarSystemValues(Key) = Value
            Catch
                mvarSystemValues.Add(Key, Value)
            End Try
        End Set
    End Property

#End Region

End Class

' other classes

#Region "Sub-Objective Maps"

<Serializable()> Public Class SubObjectiveMap
    Shared m_SubMapID As Int16
    Public m_ID As Int16
    Private m_Location As Point
    Private m_Filename As String
    Private m_Width As Int16
    Private m_Height As Int16
    Private m_Scale As String

    Public Sub New()
        m_SubMapID += 1
        m_ID = m_SubMapID
    End Sub

#Region "Properties"
    Public ReadOnly Property SubMapID() As Int16
        Get
            Return m_ID
        End Get
    End Property

    Public Property Location() As Point
        Get
            Return m_Location
        End Get
        Set(ByVal Value As Point)
            m_Location = Value
        End Set
    End Property

    Public Property FileName() As String
        Get
            Return m_Filename
        End Get
        Set(ByVal Value As String)
            m_Filename = Value
        End Set
    End Property

    Public Property Width() As Int16
        Get
            Return m_Width
        End Get
        Set(ByVal Value As Int16)
            m_Width = Value
        End Set
    End Property

    Public Property Height() As Int16
        Get
            Return m_Height
        End Get
        Set(ByVal Value As Int16)
            m_Height = Value
        End Set
    End Property

    Public Property Scale() As String
        Get
            Return m_Scale
        End Get
        Set(ByVal Value As String)
            m_Scale = Value
        End Set
    End Property
#End Region

End Class

<Serializable()> Public Class SubObjectiveMaps
    ' a collection of sub objective maps
    Private mCol As New Hashtable()

    Public Function Item(ByVal sKey As String) As DeploymentZone
        Try
            Return mCol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in DeploymentZones Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As DeploymentZone, ByVal sKey As String)
        mCol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mCol
        End Get
    End Property

End Class

#End Region

#Region "Chits"
<Serializable()> Public MustInherit Class ChitType

    Shared mvarCID As Int16
    Private mvarID As Int16
    Private mvarMapID As Int16
    Private mvarLocation As Point
    Private mvarName As String
    Private mvarDescription As String
    Private mvarIcons As New Hashtable(5)   ' Icons are: Owner, SameFaction, Allied, Neutral, Enemy
    Private mvarOwningFaction As Int64 = 0
    Private mvarOwningPlayer As Int64 = 0
    Private mvarExtendedProperties As String = "NONE"
    Private mvarInitialPointValue As Int64 = 0
    Private mvarCurrentPointValue As Int64 = 0

    Public Sub New()
        mvarIcons.Add("Owner", New Bitmap(16, 16))
        mvarIcons.Add("SameFaction", New Bitmap(16, 16))
        mvarIcons.Add("Allied", New Bitmap(16, 16))
        mvarIcons.Add("Neutral", New Bitmap(16, 16))
        mvarIcons.Add("Enemy", New Bitmap(16, 16))
        mvarCID += 1  ' increment the shared group id counter
        mvarID = mvarCID
    End Sub

    Public Overridable Function PropertiesToTreeView() As System.Windows.Forms.TreeNodeCollection
        ' returns a treenode collection of its properties
        Dim nodeCol As System.Windows.Forms.TreeNodeCollection

        nodeCol.Add("Name: " & mvarName)
        nodeCol.Add("X: " & mvarLocation.X.ToString)
        nodeCol.Add("Y: " & mvarLocation.Y.ToString)

        Return nodeCol

    End Function

    Public Sub BoostMCID()
        ' used to boost up the member class id, and the member id
        mvarCID += 100
        mvarID += 100
    End Sub

    Public Overridable ReadOnly Property Key() As String
        Get
            Return CStr(cmplib01.CampaignerGameData.CDGKEY_CHITTYPE & mvarID)
        End Get
    End Property

    Public ReadOnly Property [ID]() As Int16
        Get
            Return mvarID
        End Get
    End Property

    Public Property MapID() As Int16
        Get
            Return mvarMapID
        End Get
        Set(ByVal Value As Int16)
            mvarMapID = Value
        End Set
    End Property

    Public Property Location() As Point
        Get
            Return mvarLocation
        End Get
        Set(ByVal Value As Point)
            mvarLocation = Value
        End Set
    End Property

    Public Property [Name]() As String
        Get
            Return mvarName
        End Get
        Set(ByVal Value As String)
            mvarName = Value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return mvarDescription
        End Get
        Set(ByVal Value As String)
            mvarDescription = Value
        End Set
    End Property

    Public Property Icon(ByVal key As String) As Bitmap
        Get
            Return mvarIcons(key)
        End Get
        Set(ByVal Value As Bitmap)
            mvarIcons(key) = Value
        End Set
    End Property

    Public Property OwningFaction() As Int64
        Get
            Return mvarOwningFaction
        End Get
        Set(ByVal Value As Int64)
            mvarOwningFaction = Value
        End Set
    End Property

    Public Property OwningPlayer() As Int64
        Get
            Return mvarOwningPlayer
        End Get
        Set(ByVal Value As Int64)
            mvarOwningPlayer = Value
        End Set
    End Property

    Public Property ExtendedProperties() As String
        Get
            Return mvarExtendedProperties
        End Get
        Set(ByVal Value As String)
            mvarExtendedProperties = Value
        End Set
    End Property

    Public Property InitialPointValue() As Int64
        Get
            Return mvarInitialPointValue
        End Get
        Set(ByVal Value As Int64)
            mvarInitialPointValue = Value
        End Set
    End Property

    Public Property CurrentPointValue() As Int64
        Get
            Return mvarCurrentPointValue
        End Get
        Set(ByVal Value As Int64)
            mvarCurrentPointValue = Value
        End Set
    End Property

End Class

<Serializable()> Public MustInherit Class GroundChitType
    ' Status has been moved here for combat purposes.
    ' added battle result also.
    Inherits ChitType

    Shared mvarCID As Int16
    Private mvarTypeID As Int16
    Private mvarTypeName As String
    Private mvarLOS As Int16
    Private mvarLOSPower As Int16
    Private mvarSize As Int16
    Private mvarUsesZOC As Boolean = False
    Private mvarZOCRange As Int16 = 0
    Private mvarGroundChitClass As GroundChitClass_cgd
    <NonSerialized()> Private mvarCurrentTerrainName As String
    <NonSerialized()> Private mvarDetectedThisTurn As Boolean = False

#If aflegacy = 1 Then
    <NonSerialized()> Private mvarBattleInvolvedInID As Int16 ' this points to the combat this ground chit is involved in
    <NonSerialized()> Private mvarHistoricalLocation As New ArrayList(10)
    <NonSerialized()> Private mvarCurrentBattlePosture As BattlePosture_cgd = BattlePosture_cgd.NoBattle
    <NonSerialized()> Private mvarStatus As GroundUnitStatus_cgd ' in case the ground chit is engaged in combat.
    <NonSerialized()> Private mvarBattleResult As UnitBattleResult_cgd
#Else
    Private BattleInvolvedInID As Int16 ' this points to the combat this ground chit is involved in
    Private mvarHistoricalLocation As New ArrayList(10)
    private mvarCurrentBattlePosture as BattlePosture_cgd =BattlePosture_cgd.NoBattle
    private mvarStatus as GroundUnitStatus_cgd
    private mvarBattleresult as UnitBattleResult_cgd
#End If

    Public Sub New()
        mvarCID += 1
        mvarTypeID = mvarCID

    End Sub

    Public Property CurrentTerrainName() As String
        Get
            'used as a temporary holder
            Return mvarCurrentTerrainName
        End Get
        Set(ByVal Value As String)
            mvarCurrentTerrainName = Value
        End Set
    End Property

    Public Property Status() As GroundUnitStatus_cgd
        Get
            Return mvarStatus
        End Get
        Set(ByVal Value As GroundUnitStatus_cgd)
            mvarStatus = Value
        End Set
    End Property

    Public Property BattleResult() As UnitBattleResult_cgd
        Get
            Return mvarBattleResult
        End Get
        Set(ByVal Value As UnitBattleResult_cgd)
            mvarBattleResult = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return mvarTypeName
    End Function

    Public Overridable Property CurrentBattlePosture() As BattlePosture_cgd
        Get
            Return mvarCurrentBattlePosture
        End Get
        Set(ByVal Value As BattlePosture_cgd)
            mvarCurrentBattlePosture = Value
        End Set
    End Property

    Public Overridable Property BattleInvolvedInID() As Int16
        Get
            Return mvarBattleInvolvedInID
        End Get
        Set(ByVal Value As Int16)
            mvarBattleInvolvedInID = Value
        End Set
    End Property

    Public Property DetectedThisTurn() As Boolean
        Get
            Return mvarDetectedThisTurn
        End Get
        Set(ByVal Value As Boolean)
            mvarDetectedThisTurn = Value
        End Set
    End Property

    Public Property HistoricalLocation() As ArrayList
        Get
            Return mvarHistoricalLocation
        End Get
        Set(ByVal Value As ArrayList)
            mvarHistoricalLocation = Value
        End Set
    End Property

    Public Overrides ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CGDKEY_GROUNDCHITTYPE & mvarTypeID.ToString
        End Get
    End Property

    Public ReadOnly Property TypeID() As Int16
        Get
            Return mvarTypeID
        End Get
    End Property

    Public Property TypeName() As String
        Get
            Return mvarTypeName
        End Get
        Set(ByVal Value As String)
            mvarTypeName = Value
        End Set
    End Property

    Public Property LOS() As Int16
        Get
            Return mvarLOS
        End Get
        Set(ByVal Value As Int16)
            mvarLOS = Value
        End Set
    End Property

    Public Property LOSPower() As Int16
        Get
            Return mvarLOSPower
        End Get
        Set(ByVal Value As Int16)
            mvarLOSPower = Value
        End Set
    End Property

    Public Property Size() As Int16
        Get
            Return mvarSize
        End Get
        Set(ByVal Value As Int16)
            mvarSize = Value
        End Set
    End Property

    Public Property UsesZOC() As Boolean
        Get
            Return mvarUsesZOC
        End Get
        Set(ByVal Value As Boolean)
            mvarUsesZOC = Value
        End Set
    End Property

    Public Property ZOCRange() As Int16
        Get
            Return mvarZOCRange
        End Get
        Set(ByVal Value As Int16)
            mvarZOCRange = Value
        End Set
    End Property

    Public Property GroundChitClass() As GroundChitClass_cgd
        Get
            Return mvarGroundChitClass
        End Get
        Set(ByVal Value As GroundChitClass_cgd)
            mvarGroundChitClass = Value
        End Set
    End Property

End Class

<Serializable()> Public Class ObjectiveType
    Inherits GroundChitType
    Implements ICloneable

    Private mvarObjectiveRules As New Rules()
    Private mvarResources As New Resources()
#If aflegacy = 1 Then
    <NonSerialized()> Private mvarDetected As Boolean = False    ' once an objective is detected, its always detected
#Else
Private mvarDetected As Boolean = False
#End If

    Public Sub ResetRuleCount(ByVal ruleTs As RuleTypes)
        ' resets the max rulecount and such 
        Dim key As String
        Dim ruleTKey As String
        Dim RuleT As RuleType
        Dim rul As Rule

        Try
            For Each key In mvarObjectiveRules.HT.Keys
                rul = mvarObjectiveRules.Item(key)
                ruleTKey = CampaignerGameData.CDGKEY_RULETYPE & rul.ruleTypeID
                RuleT = ruleTs.Item(ruleTKey)
                If RuleT.MaxUses > 0 Then rul.currentUses = RuleT.MaxUses
            Next
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Public Sub GenerateResources()
        ' add resources to pool
        Dim Res As Resource
        Dim ResKey As String

        For Each ResKey In mvarResources.HT.Keys
            Res = mvarResources.Item(ResKey)
            If Res.AccrueAmount > 0 Then
                Res.CurrentAmount += Res.AccrueAmount
                If Res.CurrentAmount > Res.MaxAmount Then Res.CurrentAmount = Res.MaxAmount
            End If
        Next

    End Sub

    Public Overrides Function propertiestotreeview() As System.Windows.Forms.TreeNodeCollection
        Dim nodeCol As System.Windows.Forms.TreeNodeCollection

        nodeCol.Add("Type: " & MyBase.TypeName)
        nodeCol.Add("LOS: " & MyBase.LOS)
        nodeCol.Add("LOS Power: " & MyBase.LOSPower)
        nodeCol.Add("Size: " & MyBase.Size)
        nodeCol.Add("Uses ZOC: " & MyBase.UsesZOC.ToString)
        If MyBase.UsesZOC Then
            nodeCol.Add("ZOC: " & MyBase.ZOCRange)
        End If

        ' now put in rules and resources
        If mvarObjectiveRules.Count <> 0 Then
            Dim ruleCol As New System.Windows.Forms.TreeNode("Rules")
            Dim rul As Rule
            Dim key As String

            For Each key In mvarObjectiveRules.HT.Keys
                ruleCol.Nodes.Add("junk")
                rul = mvarObjectiveRules.Item(key)
                ruleCol.Nodes.Add(rul.ruleName)
            Next
            nodeCol.Add(ruleCol)
        End If

        If mvarResources.Count <> 0 Then

        End If

    End Function

    Public Property Detected() As Boolean
        Get
            Return mvarDetected
        End Get
        Set(ByVal Value As Boolean)
            mvarDetected = Value
        End Set
    End Property

    Public Property ObjectiveRules() As Rules
        Get
            Return mvarObjectiveRules
        End Get
        Set(ByVal Value As Rules)
            mvarObjectiveRules = Value
        End Set
    End Property

    Public Property ObjectiveResources() As Resources
        Get
            Return mvarResources
        End Get
        Set(ByVal Value As Resources)
            mvarResources = Value
        End Set
    End Property

    Public Overrides ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CGDKEY_OBJECTIVETYPE & MyBase.ID
        End Get
    End Property

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim aClone As New cmplib01.ObjectiveType()
        Dim ms As New MemoryStream(1000)
        Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))

        ' serialize object into stream
        bf.Serialize(ms, Me)

        ' position stream pointer back to first byte
        ms.Seek(0, SeekOrigin.Begin)

        ' deserialize into the return object
        aClone = bf.Deserialize(ms)

        ' release memory
        ms.Close()

        Return aClone
    End Function
End Class

<Serializable()> Public Class UnitType
    Inherits GroundChitType
    Private mvarMovementClassID As Int16
    Private mvarMovementSpeed As Int16
    Private mvarOrder As String
    Private mvarWaypoints As New ArrayList(6) ' array of points
    <NonSerialized()> Private mvarMoveRemaining As Int16 ' this is used only during the movementphase
#If aflegacy = 1 Then
    '    <NonSerialized()> Private mvarBattleResult As UnitBattleResult_cgd = UnitBattleResult_cgd.NoBattle
    Private mvarStatus As GroundUnitStatus_cgd  ' only use this in legacy
#Else
    'Private mvarBattleResult As UnitBattleResult_cgd = UnitBattleResult_cgd.NoBattle
#End If

    Public Property MoveRemaining() As Int16
        Get
            Return mvarMoveRemaining
        End Get
        Set(ByVal Value As Int16)
            mvarMoveRemaining = Value
        End Set
    End Property

    Public Property MovementClassID() As Int16
        Get
            Return mvarMovementClassID
        End Get
        Set(ByVal Value As Int16)
            mvarMovementClassID = Value
        End Set
    End Property

    Public Property MovementSpeed() As Int16
        Get
            Return mvarMovementSpeed
        End Get
        Set(ByVal Value As Int16)
            mvarMovementSpeed = Value
        End Set
    End Property

    Public Overrides Property BattleInvolvedInID() As Int16
        Get
            Return MyBase.BattleInvolvedInID
        End Get
        Set(ByVal Value As Int16)
            MyBase.BattleInvolvedInID = Value
            ' we clear all waypoints if we end up in battle.
            mvarWaypoints.Clear()
        End Set
    End Property

    Public Property Order() As String
        Get
            Return mvarOrder
        End Get
        Set(ByVal Value As String)
            mvarOrder = Value
        End Set
    End Property

    Public Property Waypoint(ByVal Index As Int32) As Point
        Get
            Try
                Return mvarWaypoints(Index)
            Catch
                Debug.WriteLine("Index out of bounds in UnitType.Waypoint")
            End Try
        End Get
        Set(ByVal Value As Point)
            Try
                mvarWaypoints(Index) = Value
            Catch
                Debug.WriteLine("Index out of bounds in UnitType.Waypoint")
            End Try
        End Set
    End Property

    Public Property Waypoints() As ArrayList
        Get
            Return mvarWaypoints
        End Get
        Set(ByVal Value As ArrayList)
            mvarWaypoints = Value
        End Set
    End Property

    Public Overrides ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_UNITTYPE & MyBase.ID
        End Get
    End Property

End Class

<Serializable()> Public Class TokenType
    Inherits UnitType

    Public TokenRule As New Rule()

    Public Overrides ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_TOKENTYPE & MyBase.ID
        End Get
    End Property

End Class
#End Region

#Region "Rules"

#If aflegacy = 1 Then
Public Class AssignedRule
    Shared mCID As Int32
    Private m_ID As Int32
    Private m_OwningChitType As GroundChitClass_cgd
    Private m_OwningChitKey As String
    Private m_BattleAssignedTo As Int32
    Private m_RuleKey As String
    Private m_DisplayName As String

    Public Sub New()
        mCID += 1
        m_ID = mCID
    End Sub

    Public Overrides Function ToString() As String
        Return m_DisplayName
    End Function

    Public ReadOnly Property Key() As String
        Get
            Return CampaignerGameData.CDGKEY_ASSIGNEDRULE & m_ID
        End Get
    End Property

    Public Property DisplayName() As String
        Get
            Return m_DisplayName
        End Get
        Set(ByVal Value As String)
            m_DisplayName = Value
        End Set
    End Property

    Public Property OwningChitType() As GroundChitClass_cgd
        Get
            Return m_OwningChitType
        End Get
        Set(ByVal Value As GroundChitClass_cgd)
            m_OwningChitType = Value
        End Set
    End Property

    Public Property OwningChitKey() As String
        Get
            Return m_OwningChitKey
        End Get
        Set(ByVal Value As String)
            m_OwningChitKey = Value
        End Set
    End Property

    Public Property BattleAssignedTo() As Int32
        Get
            Return m_BattleAssignedTo
        End Get
        Set(ByVal Value As Int32)
            Value = m_BattleAssignedTo
        End Set
    End Property

    Public Property RuleKey() As String
        Get
            Return m_RuleKey
        End Get
        Set(ByVal Value As String)
            m_RuleKey = Value
        End Set
    End Property

End Class
#Else
<Serializable()> public class AssignedRule
    Private m_OwningChitType As GroundChitClass_cgd
    Private m_OwningChitKey As String
    Private m_BattleAssignedTo As Int32
    Private m_RuleKey As String
    Private m_DisplayName As String

    Public Property DisplayName() As String
        Get
            Return m_DisplayName
        End Get
        Set(ByVal Value As String)
            m_DisplayName = Value
        End Set
    End Property

    Public Property OwningChitType() As GroundChitClass_cgd
        Get
            Return m_OwningChitType
        End Get
        Set(ByVal Value As GroundChitClass_cgd)
            m_OwningChitType = Value
        End Set
    End Property

    Public Property OwningChitKey() As String
        Get
            Return m_OwningChitKey
        End Get
        Set(ByVal Value As String)
            m_OwningChitKey = Value
        End Set
    End Property

    Public Property BattleAssignedTo() As Int32
        Get
            Return m_BattleAssignedTo
        End Get
        Set(ByVal Value As Int32)
            Value = m_BattleAssignedTo
        End Set
    End Property

    Public Property RuleKey() As String
        Get
            Return m_RuleKey
        End Get
        Set(ByVal Value As String)
            m_RuleKey = Value
        End Set
    End Property
end class

#End If


<Serializable()> Public Class RuleType
    Shared mCID As Int16
    Private mvarRuleTypeID As Int16
    Private mvarRuleName As String = "New Rule"
    Private mvarBasicRange As Int16 = 0
    Private mvarAppliesDuring As RuleApplies_cgd = RuleApplies_cgd.Constant
    Private mvarMaxUses As Int16 = 0 ' non zero denotes number of uses per turn
    Private mvarRuleText As String = "Enter a description"
    Private mvarCoreRule As CoreRules_cgd = CoreRules_cgd.None

    Public Sub New()
        mCID += 1
        mvarRuleTypeID = mCID
    End Sub

    Public Sub BoostMCID()
        ' used to boost up the member class id, and the member id
        mCID += 100
        mvarRuleTypeID += 100
    End Sub

    Public Overrides Function ToString() As String
        Return mvarRuleName
    End Function

#Region "Property Declarations"
    Public ReadOnly Property RuleTypeID() As Int16
        Get
            Return mvarRuleTypeID
        End Get
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_RULETYPE & mvarRuleTypeID
        End Get
    End Property

    Public Property RuleName() As String
        Get
            Return mvarRuleName
        End Get
        Set(ByVal Value As String)
            mvarRuleName = Value
        End Set
    End Property

    Public Property BasicRange() As Int16
        Get
            Return mvarBasicRange
        End Get
        Set(ByVal Value As Int16)
            mvarBasicRange = Value
        End Set
    End Property

    Public Property AppliesDuring() As RuleApplies_cgd
        Get
            Return mvarAppliesDuring
        End Get
        Set(ByVal Value As RuleApplies_cgd)
            mvarAppliesDuring = Value
        End Set
    End Property

    Public Property MaxUses() As Int16
        Get
            Return mvarMaxUses
        End Get
        Set(ByVal Value As Int16)
            mvarMaxUses = Value
        End Set
    End Property

    Public Property RuleText() As String
        Get
            Return mvarRuleText
        End Get
        Set(ByVal Value As String)
            mvarRuleText = Value
        End Set
    End Property

    Public Property CoreRule() As CoreRules_cgd
        Get
            Return mvarCoreRule
        End Get
        Set(ByVal Value As CoreRules_cgd)
            mvarCoreRule = Value
        End Set
    End Property

#End Region

End Class

<Serializable()> Public Class RuleTypes
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As RuleType
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in RuleTypes Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As RuleType, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class


<Serializable()> Public Class Rule
    ' this is a particular instance of a rule for an objective or token, to be stuffed into the owning object's Rules property
    Shared mCID As Int16
    Public ruleID As Int16
    Public ruleTypeID As Int16
    Public ruleName As String
    Public currentUses As Int16

    Public Sub New()
        mCID += 1
        ruleID = mCID
    End Sub

    Public Overrides Function ToString() As String
        Return ruleName
    End Function

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_RULE & ruleID.ToString
        End Get
    End Property

End Class

<Serializable()> Public Class Rules
    Private mcol As New Hashtable(20)

    Public Function Item(ByVal sKey As String) As Rule
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in Rules Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As Rule, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Resources"
<Serializable()> Public Class ResourceType
    Shared mCID As Int16
    Private mvarResourceTypeID As Int16
    Private mvarResourceName As String
    Private mvarResourceDescription As String

    Public Sub New()
        mCID += 1
        mvarResourceTypeID = mCID
    End Sub

    Public Overrides Function ToString() As String
        Return mvarResourceName
    End Function

    Public Property ResourceTypeID() As Int16
        Get
            Return mvarResourceTypeID
        End Get
        Set(ByVal Value As Int16)
            mvarResourceTypeID = Value
        End Set
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_RESOURCETYPE & mvarResourceTypeID
        End Get
    End Property

    Public Property ResourceName() As String
        Get
            Return mvarResourceName
        End Get
        Set(ByVal Value As String)
            mvarResourceName = Value
        End Set
    End Property

    Public Property ResourceDescription() As String
        Get
            Return mvarResourceDescription
        End Get
        Set(ByVal Value As String)
            mvarResourceDescription = Value
        End Set
    End Property

End Class

<Serializable()> Public Class ResourceTypes
    Private mcol As New Hashtable(20)

    Public Function Item(ByVal sKey As String) As ResourceType
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in ResourceTypes Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As ResourceType, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class

<Serializable()> Public Class Resource
    Shared mCID As Int16
    Private mvarResourceTypeID As Int16
    Private mvarMaxAmount As Int16
    Private mvarAccrues As Boolean
    Private mvarAccrueAmount As Int16
    Private mvarCurrentAmount As Int16
    Private mvarResourceName As String
    Private mvarResourceID As Int16

    Public Sub New()
        mCID += 1
        mvarResourceID = mCID
    End Sub

    Public Overrides Function ToString() As String
        Return mvarResourceName
    End Function

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_RESOURCE & mvarResourceID
        End Get
    End Property

    Public Property ResourceTypeID() As Int16
        Get
            Return mvarResourceTypeID
        End Get
        Set(ByVal Value As Int16)
            mvarResourceTypeID = Value
        End Set
    End Property

    Public Property MaxAmount() As Int16
        Get
            Return (mvarMaxAmount)
        End Get
        Set(ByVal Value As Int16)
            mvarMaxAmount = Value
        End Set
    End Property

    Public Property Accrues() As Boolean
        Get
            Return mvarAccrues
        End Get
        Set(ByVal Value As Boolean)
            mvarAccrues = Value
        End Set
    End Property

    Public Property AccrueAmount() As Int16
        Get
            Return (mvarAccrueAmount)
        End Get
        Set(ByVal Value As Int16)
            mvarAccrueAmount = Value
        End Set
    End Property

    Public Property CurrentAmount() As Int16
        Get
            Return mvarCurrentAmount
        End Get
        Set(ByVal Value As Int16)
            mvarCurrentAmount = Value
        End Set
    End Property

    Public Property ResourceName() As String
        Get
            Return mvarResourceName
        End Get
        Set(ByVal Value As String)
            mvarResourceName = Value
        End Set
    End Property

End Class

<Serializable()> Public Class Resources
    ' collection of resource objects, for use with objectives, to tally them up!
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As Resource
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in Resources Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As Resource, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Orders"
<Serializable()> Public Class OrderType
    Shared mCID As Int16
    Private mvarOrderID As Int16
    Private mvarOrderName As String
    Private mvarOrderFlag As Int16

    Public Sub New()
        mCID += 1
        mvarOrderID = mCID
        mvarOrderName = "New Order " & mvarOrderID
    End Sub

    Public Property OrderFlag() As Int16
        Get
            Return mvarOrderFlag
        End Get
        Set(ByVal Value As Int16)
            mvarOrderFlag = Value
        End Set
    End Property

    Public Sub BoostMCID()
        ' used to boost up the member class id, and the member id
        mCID += 100
        mvarOrderID += 100
    End Sub

    Public Overrides Function ToString() As String
        Return mvarOrderName
    End Function

    Public ReadOnly Property OrderID() As Int16
        Get
            Return mvarOrderID
        End Get
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_ORDER & mvarOrderID
        End Get
    End Property

    Public Property OrderName() As String
        Get
            Return mvarOrderName
        End Get
        Set(ByVal Value As String)
            mvarOrderName = Value
        End Set
    End Property

End Class

<Serializable()> Public Class OrderTypes
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As OrderType
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in OrderTypes Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As OrderType, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property
End Class
#End Region

#Region "Factions"
<Serializable()> Public Class Faction
    Shared mCID As Int16
    Private mvarFactionID As Int16
    Private mvarFactionName As String
    Private mvarFactionDescription As String
    Private mvarFactionFreePoints As Int32
    Private mvarFactionDeployedPoints As Int32
    Private mvarFactionTotalDeployedPoints As Int32   ' the total points that have been deployed, including units that are destroyed
    Private mvarFactionTotalPoints As Int32           ' the total point value given to a faction at the beginning of the game

    Private mcolChitDetection As New Hashtable(20)

    Public Function Item(ByVal sKey As String) As ChitDetection
        Try
            Return mcolChitDetection(sKey)
        Catch
            Throw New NullReferenceException()
        End Try
    End Function

    Public Sub Add(ByVal Item As ChitDetection, ByVal sKey As String)
        mcolChitDetection.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcolChitDetection.Count
        End Get
    End Property

    Public ReadOnly Property HTChitDetection() As Hashtable
        Get
            Return mcolChitDetection
        End Get
    End Property

    Public Sub New()
        mCID += 1
        mvarFactionID = mCID
    End Sub

    Public Sub New(ByVal facName As String, ByVal Descrip As String, ByVal startingPoints As Int16, ByVal freePoints As Int16, ByVal deployedPoints As Int16)
        Me.New()
        mvarFactionName = facName
        mvarFactionDescription = Descrip
        mvarFactionTotalPoints = startingPoints
        mvarFactionFreePoints = freePoints
        mvarFactionDeployedPoints = deployedPoints
    End Sub

    Public Overrides Function ToString() As String
        Return mvarFactionName
    End Function

    Public Property FactionID() As Int16
        Get
            Return mvarFactionID
        End Get
        Set(ByVal Value As Int16)
            mvarFactionID = Value
        End Set
    End Property

    Public Property FactionName() As String
        Get
            Return mvarFactionName
        End Get
        Set(ByVal Value As String)
            mvarFactionName = Value
        End Set
    End Property

    Public Property FactionDescription() As String
        Get
            Return mvarFactionDescription
        End Get
        Set(ByVal Value As String)
            mvarFactionDescription = Value
        End Set
    End Property

    Public ReadOnly Property FactionFreePoints() As Int16
        Get
            Return mvarFactionTotalPoints - mvarFactionDeployedPoints
        End Get
    End Property

    Public Property FactionDeployedPoints() As Int16
        Get
            Return mvarFactionDeployedPoints
        End Get
        Set(ByVal Value As Int16)
            mvarFactionDeployedPoints = Value
        End Set
    End Property

    Public Property FactionTotalDeployedPoints() As Int16
        Get
            Return mvarFactionTotalDeployedPoints
        End Get
        Set(ByVal Value As Int16)
            mvarFactionTotalDeployedPoints = Value
        End Set
    End Property

    Public Property FactionTotalPoints() As Int16
        Get
            Return mvarFactionTotalPoints
        End Get
        Set(ByVal Value As Int16)
            mvarFactionTotalPoints = Value
        End Set
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return cmplib01.CampaignerGameData.CDGKEY_FACTION & mvarFactionID
        End Get
    End Property

End Class

<Serializable()> Public Class Factions
    Private mcol As New Hashtable(1)

    Public Sub New()
        ' add the two basic factions
        Dim fac As New Faction("Attacker", "Attacking Force", 10000, 0, 0)
        mcol.Add(fac.Key, fac)

        Dim fac2 As New Faction("Defender", "Defending Force", 7500, 0, 0)
        mcol.Add(fac2.Key, fac2)

    End Sub

    Public Function Item(ByVal sKey As String) As Faction
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in Factions Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As Faction, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Messages"
<Serializable()> Public Class Messagem
    Private mvarMessageID As Int16
    Private mvarSendingPlayerID As Int16
    Private mvarReceivingPlayerID As Int16
    Private mvarTitle As String
    Private mvarMessage As String
    Private mvarTurnActive As Int16
    Private mvarViewed As Boolean

    Public Overrides Function ToString() As String
        Return mvarTitle
    End Function

    Public Property MessageID() As Int16
        Get
            Return mvarMessageID
        End Get
        Set(ByVal Value As Int16)
            mvarMessageID = Value
        End Set
    End Property

    Public Property SendingPlayerID() As Int16
        Get
            Return mvarSendingPlayerID
        End Get
        Set(ByVal Value As Int16)
            mvarSendingPlayerID = Value
        End Set
    End Property

    Public Property ReceivingPlayerID() As Int16
        Get
            Return mvarReceivingPlayerID
        End Get
        Set(ByVal Value As Int16)
            mvarReceivingPlayerID = Value
        End Set
    End Property

    Public Property Title() As String
        Get
            Return mvarTitle
        End Get
        Set(ByVal Value As String)
            mvarTitle = Value
        End Set
    End Property

    Public Property Message() As String
        Get
            Return mvarMessage
        End Get
        Set(ByVal Value As String)
            mvarMessage = Value
        End Set
    End Property

    Public Property TurnActive() As Int16
        Get
            Return mvarTurnActive
        End Get
        Set(ByVal Value As Int16)
            mvarTurnActive = Value
        End Set
    End Property

    Public Property Viewed() As Boolean
        Get
            Return mvarViewed
        End Get
        Set(ByVal Value As Boolean)
            mvarViewed = Value
        End Set
    End Property
End Class

<Serializable()> Public Class Messages
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As Messagem
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in Messages Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As Messagem, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property Count() As Int16
        Get
            Return mcol.Count
        End Get
    End Property

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "DeploymentZones"
<Serializable()> Public Class DeploymentZone
    Private mvarMapID As Int16
    Private mvarZoneID As Int16
    Private mvarZoneName As String
    Private mvarLocation As Point
    Private mvarActive As Boolean = False
    Private mvarFactionID As Int16 = -1
    Private mvarCapturable As Boolean = False
    Private mvarCaptureEvent As CaptureEvent_cgd = CaptureEvent_cgd.ChangeOwnership
    Private mvarCaptured As Boolean = False
    Private mvarMaxDeploymentPerTurn As Int32 = -1    ' no maximum
    Private mvarMaxTotalDeploy As Int32 = -1               ' no max
    Private mvarBoundaryZone As Boolean = False
    Private mvarPointsDeployedThisTurn As Int32
    <NonSerialized()> Private mvarBoundaryPath As System.Drawing.Drawing2D.PathData      ' of points no doubt to recreate the graphics path, if its a boundary zone
    Private boundaryPoints() As PointF ' the serializable form of pathdata
    Private mvarWidth As Int16 = 60                 ' if its not a boundary zone
    Private mvarHeight As Int16 = 60                ' if its not a boundary zone

    Friend Sub SetBoundaryPathToPoints()
        ' converts the boundarypath to a point arraylist since pathdata cannot be serialized.
        boundaryPoints = mvarBoundaryPath.Points
    End Sub

    Friend Sub SetBoundaryPointsToPath()
        ' converts from the serialized arraylist of points to the non-serializable pathdata
        mvarBoundaryPath = New System.Drawing.Drawing2D.PathData()
        mvarBoundaryPath.Points = boundaryPoints

    End Sub

    Public Overrides Function ToString() As String
        Return mvarZoneName
    End Function

    Public Property MapID() As Int16
        Get
            Return mvarMapID
        End Get
        Set(ByVal Value As Int16)
            mvarMapID = Value
        End Set
    End Property

    Public Property ZoneID() As Int16
        Get
            Return mvarZoneID
        End Get
        Set(ByVal Value As Int16)
            mvarZoneID = Value
        End Set
    End Property

    Public Property ZoneName() As String
        Get
            Return mvarZoneName
        End Get
        Set(ByVal Value As String)
            mvarZoneName = Value
        End Set
    End Property

    Public Property Location() As Point
        Get
            Return mvarLocation
        End Get
        Set(ByVal Value As Point)
            mvarLocation = Value
        End Set
    End Property

    Public Property Active() As Boolean
        Get
            Return mvarActive
        End Get
        Set(ByVal Value As Boolean)
            mvarActive = Value
        End Set
    End Property

    Public Property FactionID() As Int16
        Get
            Return mvarFactionID
        End Get
        Set(ByVal Value As Int16)
            mvarFactionID = Value
        End Set
    End Property

    Public Property Capturable() As Boolean
        Get
            Return mvarCapturable
        End Get
        Set(ByVal Value As Boolean)
            mvarCapturable = Value
        End Set
    End Property

    Public Property CaptureEvent() As CaptureEvent_cgd
        Get
            Return mvarCaptureEvent
        End Get
        Set(ByVal Value As CaptureEvent_cgd)
            mvarCaptureEvent = Value
        End Set
    End Property

    Public Property Captured() As Boolean
        Get
            Return mvarCaptured
        End Get
        Set(ByVal Value As Boolean)
            mvarCaptured = Value
        End Set
    End Property

    Public Property MaxDeploymentPerTurn() As Int32
        Get
            Return mvarMaxDeploymentPerTurn
        End Get
        Set(ByVal Value As Int32)
            mvarMaxDeploymentPerTurn = Value
        End Set
    End Property

    Public Property MaxTotalDeploy() As Int32
        Get
            Return mvarMaxTotalDeploy
        End Get
        Set(ByVal Value As Int32)
            mvarMaxTotalDeploy = Value
        End Set
    End Property

    Public Property Width() As Int16
        Get
            Return mvarWidth
        End Get
        Set(ByVal Value As Int16)
            mvarWidth = Value
        End Set
    End Property

    Public Property Height() As Int16
        Get
            Return mvarHeight
        End Get
        Set(ByVal Value As Int16)
            mvarHeight = Value
        End Set
    End Property

    Public Property BoundaryZone() As Boolean
        Get
            Return mvarBoundaryZone
        End Get
        Set(ByVal Value As Boolean)
            mvarBoundaryZone = Value
        End Set
    End Property

    Public Property BoundaryPath() As System.Drawing.Drawing2D.PathData
        Get
            Return mvarBoundaryPath
        End Get
        Set(ByVal Value As System.Drawing.Drawing2D.PathData)
            mvarBoundaryPath = Value
        End Set
    End Property

    Public ReadOnly Property BoundaryGraphicsPath() As System.Drawing.Drawing2D.GraphicsPath
        Get
            ' used to return a graphics path of this deployment zone
            Dim graphPath As New System.Drawing.Drawing2D.GraphicsPath()
            Dim lastPnt As PointF
            Dim pnt As PointF
            Dim i As Int32

            If boundaryPoints.GetUpperBound(0) <= 1 Then
                Return Nothing
            End If

            For i = 1 To boundaryPoints.GetUpperBound(0) - 1
                lastPnt = boundaryPoints(i - 1)
                pnt = boundaryPoints(i)
                graphPath.AddLine(lastPnt, pnt)
            Next

            Return graphPath
        End Get
    End Property

    Public Property PointsDeployedThisTurn() As Int32
        Get
            Return mvarPointsDeployedThisTurn
        End Get
        Set(ByVal Value As Int32)
            mvarPointsDeployedThisTurn = Value
        End Set
    End Property

End Class

<Serializable()> Public Class DeploymentZones
    Private mcol As New Hashtable()

    Friend Sub SetPathToPoints()
        Dim dep As DeploymentZone
        Dim key As String

        For Each key In mcol.Keys
            dep = mcol.Item(key)
            dep.SetBoundaryPathToPoints()
        Next
    End Sub

    Friend Sub SetPointsToPath()
        Dim dep As DeploymentZone
        Dim key As String

        For Each key In mcol.Keys
            dep = mcol.Item(key)
            dep.SetBoundaryPointsToPath()
        Next
    End Sub

    Public Function Item(ByVal sKey As String) As DeploymentZone
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in DeploymentZones Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As DeploymentZone, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Battles"
<Serializable()> Public Class Battle

    Shared mCID As Int16
    Private mvarBattleID As Int16
    Private mvarAttackingChitInitiative As Int16
    Private mvarDefendingChitInitiative As Int16
    Private mvarAttackingFactionID As Int32
    Private mvarDefendingFactionID As Int32
    Private mvarBattleResult As BattleResult_cgd
    Private mvarBattleName As String
    Private mvarBattleLocation As Point
    Private mvarInvolvedChits As New Hashtable(2)
    Private mvarAttackerAssignedRules As New Hashtable(2) ' The object type is ASSIGNEDRULE
    Private mvarDefenderAssignedRules As New Hashtable(2) ' same here.

    Public Sub New()
        mCID += 1
        mvarBattleID = mCID
    End Sub

    Public Overrides Function ToString() As String
        Return "Battle " & mvarBattleName
    End Function

    Public Property AttackingFactionID() As Int32
        Get
            Return mvarAttackingFactionID
        End Get
        Set(ByVal Value As Int32)
            mvarAttackingFactionID = Value
        End Set
    End Property

    Public Property DefendingFactionID() As Int32
        Get
            Return mvarDefendingFactionID
        End Get
        Set(ByVal Value As Int32)
            mvarDefendingFactionID = Value
        End Set
    End Property

    Public Property AttackerAssignedRules() As Hashtable
        Get
            Return mvarAttackerAssignedRules
        End Get
        Set(ByVal Value As Hashtable)
            mvarAttackerAssignedRules = Value
        End Set
    End Property

    Public Property DefenderAssignedRules() As Hashtable
        Get
            Return mvarDefenderAssignedRules
        End Get
        Set(ByVal Value As Hashtable)
            mvarDefenderAssignedRules = Value
        End Set
    End Property

    Public Property InvolvedChits() As Hashtable
        Get
            Return mvarInvolvedChits
        End Get
        Set(ByVal Value As Hashtable)
            mvarInvolvedChits = Value
        End Set
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return CampaignerGameData.CDGKEY_BATTLE & mvarBattleID
        End Get
    End Property

    Public Property BattleLocation() As Point
        Get
            Return mvarBattleLocation
        End Get
        Set(ByVal Value As Point)
            mvarBattleLocation = Value
        End Set
    End Property

    Public Property BattleName() As String
        Get
            Return mvarBattleName
        End Get
        Set(ByVal Value As String)
            mvarBattleName = Value
        End Set
    End Property

    Public Property BattleID() As Int64
        Get
            Return mvarBattleID
        End Get
        Set(ByVal Value As Int64)
            mvarBattleID = Value
        End Set
    End Property

    Public Property AttackingChitInitiative() As Int16
        Get
            Return mvarAttackingChitInitiative
        End Get
        Set(ByVal Value As Int16)
            mvarAttackingChitInitiative = Value
        End Set
    End Property

    Public Property DefendingChitInitiative() As Int16
        Get
            Return mvarDefendingChitInitiative
        End Get
        Set(ByVal Value As Int16)
            mvarDefendingChitInitiative = Value
        End Set
    End Property

    Public Property BattleResult() As BattleResult_cgd
        Get
            Return mvarBattleResult
        End Get
        Set(ByVal Value As BattleResult_cgd)
            mvarBattleResult = Value
        End Set
    End Property

    Public ReadOnly Property BattleResultString() As String
        Get
            Select Case mvarBattleResult
                Case BattleResult_cgd.AttackerMarginalVictory : Return "Attacker Marginal Victory"
                Case BattleResult_cgd.AttackerVictory : Return "Attacker Victory"
                Case BattleResult_cgd.DefenderMarginalVictory : Return "Defender Marginal Victory"
                Case BattleResult_cgd.DefenderVictory : Return "Defender Victory"
                Case BattleResult_cgd.Draw : Return "Draw"
                Case BattleResult_cgd.NotFinished : Return "Not Finished"
            End Select
        End Get

    End Property

End Class

<Serializable()> Public Class Battles
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As Battle
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in Battles Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As Battle, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Diplomacy"
<Serializable()> Public Class Diplomacy
    ' each faction should be represented, but for now, everyone HATES everyone!!! hahahahahahaha

    Public Property DiplomaticStanding(ByVal Faction1 As Int16, ByVal Faction2 As Int16) As DiplomaticStanding_cgd
        Get
            Return DiplomaticStanding_cgd.Enemy
        End Get
        Set(ByVal Value As DiplomaticStanding_cgd)

        End Set
    End Property
End Class
#End Region

#Region "Map Labels"
<Serializable()> Public Class MapLabel
    Private mvarMapLabelID As Int16
    Public LabelText As String
    Public LabelColor As Color = Color.WhiteSmoke
    Public Location As Point
    Public MapID As Int16
    Public Visible As Boolean = True

    Public Overrides Function ToString() As String
        Return LabelText
    End Function

    Public Property MapLabelID() As Int16
        Get
            Return mvarMapLabelID
        End Get
        Set(ByVal Value As Int16)
            mvarMapLabelID = Value
        End Set
    End Property

End Class

<Serializable()> Public Class MapLabels
    Private mcol As New Hashtable()

    Public Function Item(ByVal sKey As String) As MapLabel
        Try
            Return mcol(sKey)
        Catch
            Debug.WriteLine("Could not find item '" & sKey & "' in MapLabels Collection.")
        End Try
    End Function

    Public Sub Add(ByVal Item As MapLabel, ByVal sKey As String)
        mcol.Add(sKey, Item)
    End Sub

    Public ReadOnly Property HT() As Hashtable
        Get
            Return mcol
        End Get
    End Property

End Class
#End Region

#Region "Player"
<Serializable()> Public Class Player
    Shared mCID As Int64
    Private m_PlayerID
    Private m_PlayerName As String
    Private m_Password As String
    Private m_FactionKey As String
    Private m_TurnComplete As Boolean

    Public Sub New()
        Dim rand As New Random(CInt(Date.Now.Second And Integer.MaxValue))
        mCID += 1
        m_PlayerID = rand.Next
    End Sub

    Public Overrides Function ToString() As String
        Return m_PlayerName
    End Function

    Public Function Key() As String
        Return CampaignerGameData.CDGKEY_PLAYER & m_PlayerID
    End Function

    Public Property PlayerID() As Int64
        Get
            Return m_PlayerID
        End Get
        Set(ByVal Value As Int64)
            m_PlayerID = Value
        End Set
    End Property

    Public Property PlayerName() As String
        Get
            Return m_PlayerName
        End Get
        Set(ByVal Value As String)
            m_PlayerName = Value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return m_Password
        End Get
        Set(ByVal Value As String)
            m_Password = Value
        End Set
    End Property

    Public Property FactionKey() As String
        Get
            Return m_FactionKey
        End Get
        Set(ByVal Value As String)
            m_FactionKey = Value
        End Set
    End Property

    Public Property TurnComplete() As Boolean
        Get
            Return m_TurnComplete
        End Get
        Set(ByVal Value As Boolean)
            m_TurnComplete = Value
        End Set
    End Property

End Class

#End Region

#Region "Chit Detection"
<Serializable()> Public Class ChitDetection
    Private m_ChitKey As String
    Private m_DetectionLevel As Int16
    Private m_ChitDetectedThisTurn As Boolean = False

    Public Property ChitKey() As String
        Get
            Return m_ChitKey
        End Get
        Set(ByVal Value As String)
            m_ChitKey = Value
        End Set
    End Property

    Public Property DetectionLevel() As Int16
        Get
            Return m_DetectionLevel
        End Get
        Set(ByVal Value As Int16)
            m_DetectionLevel = Value
        End Set
    End Property

    Public Property ChitDetectedThisTurn() As Boolean
        Get
            Return m_ChitDetectedThisTurn
        End Get
        Set(ByVal Value As Boolean)
            m_ChitDetectedThisTurn = Value
        End Set
    End Property

End Class
#End Region

#Region "LogEvent"
<Serializable()> Public Class LogEvent
    Private m_InvokingPlayerName As String
    Private m_Action As String
    Private m_ExtendedProperties As String
    Private m_AuthorityLevel As EventAuthorityLevel
    Private m_Acknowledged As Boolean
    Private m_TurnNumber As Int32
    Private m_Key As String

    Public Sub New()
        '// generates a unique (hopefully) four number and six letter logevent key
        Dim rand As New Random()
        Dim i As Int16
        Dim c As Char
        Dim count As Byte
        Dim strKey As String

        For count = 1 To 4
            i = rand.Next(65, 90)
            c = Chr(i)
            strKey = strKey & c
        Next
        i = rand.Next(1000, 9999)
        strKey = strKey & i.ToString

        m_Key = strKey
    End Sub

    Public ReadOnly Property Key() As String
        Get
            Return m_Key
        End Get
    End Property

    Public Property TurnNumber() As Int32
        Get
            Return m_TurnNumber
        End Get
        Set(ByVal Value As Int32)
            m_TurnNumber = Value
        End Set
    End Property

    Public Property AuthorityLevel() As EventAuthorityLevel
        Get
            Return m_AuthorityLevel
        End Get
        Set(ByVal Value As EventAuthorityLevel)
            m_AuthorityLevel = Value
        End Set
    End Property

    Public Property InvokingPlayerName() As String
        Get
            Return m_InvokingPlayerName
        End Get
        Set(ByVal Value As String)
            m_InvokingPlayerName = Value
        End Set
    End Property

    Public Property Action() As String
        Get
            Return m_Action
        End Get
        Set(ByVal Value As String)
            m_Action = Value
        End Set
    End Property

    Public Property ExtendedProperties() As String
        Get
            Return m_ExtendedProperties
        End Get
        Set(ByVal Value As String)
            m_ExtendedProperties = Value
        End Set
    End Property

    Public Property Acknowledged() As Boolean
        Get
            Return m_Acknowledged
        End Get
        Set(ByVal Value As Boolean)
            m_Acknowledged = Value
        End Set
    End Property

End Class
#End Region



